local GameDB = import(".GameDB")


-- partner2 = {key = "ID"},
-- 	partner_quality2 = {key = "ID"},
-- 	partner_lv = {key = "ID"},
-- 	partner_equip = {key = "ID"},
-- 	partner_equipenhance = {key = "ID"},
-- 	partner_skill = {key = "ID"},
-- 	partner_relation = {key = "ID"},
-- 	partner_relationscore = {key = "ID"},

-- 获取 随从品质的 判断标准
function GameDB:queryPartnetQualityConfig()
    local t = self:getCachedValues("partner_quality2")
    local configs = {}
    for k ,v in ipairs(t) do
        local id = tonumber(v.ID)
        table.insert(configs, {id = id, powerMin = tonumber(v.powerMin), powerMax = tonumber(v.powerMax),
            relationScore = tonumber(v.relationScore), qualityIcon = v.qualityIcon, qualityString = v.qualityString,
        qualityStringColor = v.qualityStringColor})
    end
   
    table.sort(configs, function(a,b)
        return a.id < b.id
    end)
    configs[#configs].isMax = true
    return configs
end


-- 获取随从 当前等级升级上限
function GameDB:queryPartnetLevelUpExpConfig(level)
    local lvStr = tostring(level)
    local r = self:getCachedValue("partner_lv", function(r)
        if r.ID == lvStr then
            return true
        end
    end)

    if not r  then
        return 0, true
    end
    return tonumber(r.exp), false
end

-- 根据装备id 获取配置信息
function GameDB:queryPartnerEquipById(id)
    local idStr = tostring(id)
    local r = self:getCachedValue("partner_equip", function(r)
        if r.ID == idStr then
            return true
        end
    end)
    assert(r ~= nil , string.format("queryPartnerEquipById id = %s is nil", id or "nil"))
    return r
end

function GameDB:getPartnerEquips()
    return self:getCachedValues("partner_equip")
end
--
function GameDB:queryPartnerEquipStrengthenByLevel(lv)
    local lvStr = tostring(lv)
    local r = self:getCachedValue("partner_equipenhance", function(r)
        if r.ID == lvStr then
            return true
        end
    end)
    assert(r ~= nil , string.format("queryPartnerEquipStrengthenByLevel lv = %s is nil", lv or "nil"))
    return r
end

-- 

function GameDB:queryPartnerById(id)
    local idStr = tostring(id)
    local r = self:getCachedValue("partner2", function(r)
        if r.ID == idStr then
            return true
        end
    end)
    assert(r ~= nil , string.format("queryPartnerById id = %s is nil", id or "nil"))
    return r
end


function GameDB:queryPartnerByRace(raceId)
    local r = self:getCachedValue("partner2", function(r)
        if r.raceID == tostring(raceId) then
            return true
        end
    end)
    return r
end

function GameDB:getPartners()
    return self:getCachedValues("partner2")
end


function GameDB:queryPartnerBySkillById(id)
    local idStr = tostring(id)
    local r = self:getCachedValue("partner_skill", function(r)
        if r.ID == idStr then
            return true
        end
    end)
    assert(r ~= nil , "queryPartnerBySkillById is nil  id = "..(idStr or "nil"))
    return r
end

function GameDB:queryPartnerQualityPropById(id)
    local idStr = tostring(id)
    local r = self:getCachedValue("partner_qualityprop", function(r)
        if r.ID == idStr then
            return true
        end
    end)
    assert(r ~= nil , string.format("queryPartnerpropById id = %s is nil", id or "nil"))
    return r
end

-- function GameDB:queryPartnerBySkillByPartnerIdAndSlotAndLv(partnerId, slot, lv)
--     local partnerIdStr = tostring(partnerId)
--     local lvStr = tostring(lv)
--     local slotStr = tostring(slot)
--     local r = self:getCachedValue("partner_skill", function(r)
--         if r.belongTo == partnerIdStr and r.sort == slotStr and  r.lv == lvStr then
--             return true
--         end
--     end)
--     return r
-- end


function GameDB:queryPartnerSkillsByPartnerIdAndLv(partnerId, lv)
    local partnerIdStr = tostring(partnerId)
    local lvStr = tostring(lv)
    local rows = self:getCachedValues("partner_skill", function(r)
        if r.belongTo == partnerIdStr  and  r.lv == lvStr then
            return true
        end
    end)
    return rows
end

function GameDB:queryPartnerSkillsByPartnerId(partnerId)
    local partnerIdStr = tostring(partnerId)
    local rows = self:getCachedValues("partner_skill", function(r)
        if r.belongTo == partnerIdStr then
            return true
        end
    end)
 
    local slotMap = {}
    for k , v in pairs(rows) do
        local num = tonumber(v.sort)
        local minLevelConf = slotMap[num]
        if not minLevelConf then
            slotMap[num] = v
        else
            if tonumber(minLevelConf.lv) > tonumber(v.lv) then
                slotMap[num] = v
            end
        end
    end
    return slotMap
end

function GameDB:queryPartnerRelationById(id)
    local IdStr = tostring(id)
    local r = self:getCachedValue("partner_relation", function(r)
        if r.ID == IdStr then
            return true
        end
    end)
    assert(r ~= nil , "queryPartnerRelationById is nil  id = "..(idStr or "nil"))
    return r
end

function GameDB:getPartnerRelation()
    return self:getCachedValues("partner_relation")
end

function GameDB:getPartnerRelationScore()
    return self:getCachedValues("partner_relationscore")
end

function GameDB:getPartnerSoulConf(id)
    local conf = self:getCachedValue("partner2_soul",id)
    return conf
end

-- function GameDB:
